package com.thomas.jaspersoft.studio.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class ResourceUtil {
	/**
	 * 
	 * @param path   the path could be begin with classpath*:
	 * @return
	 * @throws IOException 
	 */
	public static Resource[] getResourceFromPath(String path) throws IOException{
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource[] resources = resolver.getResources(path);
		return resources;
	}
	
	public static InputStream[] getInputStream(String path) throws IOException{
		Resource[] resources = getResourceFromPath(path);
		List<InputStream> inputStreams = new ArrayList<InputStream>();
		for(Resource resource : resources){
			inputStreams.add(resource.getInputStream());
		}
		return (InputStream[]) inputStreams.toArray();
	}
	
	public static Resource getSingleResourceFromPath(String path) throws IOException{
		Resource[] resources = getResourceFromPath(path);
		return resources.length > 0 ? resources[0]: null;
	}
	
	public static InputStream getSingleInputStream(String path) throws IOException{
		Resource resource = getSingleResourceFromPath(path);
		return resource.getInputStream();
	}
}
